package edu.gatech.cs2340.bytebankers.view;

import java.awt.Color;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import edu.gatech.cs2340.bytebankers.presenter.IRiverPresenter;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;

public class RiverView extends JPanel implements IRiverView {
    private IRiverPresenter presenter;
    private double depth;
    private int choice;
    private JButton btnFordRiver;
    private JButton btnTakeFerry;
    private JPanel infoPanel;
    private JLabel lblRiverDepth;
    private JLabel lblMoney;
    private JLabel lblNewLabel;
    private double moneyLeft;
    private JLabel lblRiverName;
    private JLabel lblNewLabel_1;
    private JLabel lblNewLabel_2;
    private JLabel riverimage;

    JButton btnCaulkRiver;

    public RiverView(IRiverPresenter presenter) {
	this.presenter = presenter;


	setLayout(null);

	btnCaulkRiver = new JButton("Caulk River");
	btnCaulkRiver.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent arg0) {
	    }
	});
	btnCaulkRiver.setBounds(20, 436, 130, 23);
	add(btnCaulkRiver);
	btnCaulkRiver.addActionListener(new ButtonListener());

	btnFordRiver = new JButton("Ford River");
	btnFordRiver.setBounds(150, 436, 130, 23);
	add(btnFordRiver);
	btnFordRiver.addActionListener(new ButtonListener());

	infoPanel = new JPanel();
	infoPanel.setBackground(Color.DARK_GRAY);
	infoPanel.setBounds(20, 70, 222, 101);
	add(infoPanel);
	infoPanel.setLayout(null);

	lblRiverDepth = new JLabel("River Depth:");
	lblRiverDepth.setForeground(Color.white);
	lblRiverDepth.setBounds(10, 44, 90, 14);
	infoPanel.add(lblRiverDepth);

	lblMoney = new JLabel("Money:");
	lblMoney.setForeground(Color.white);
	lblMoney.setBounds(10, 69, 90, 19);
	infoPanel.add(lblMoney);

	lblRiverName = new JLabel("River Name:");
	lblRiverName.setForeground(Color.white);
	lblRiverName.setBounds(10, 13, 90, 20);
	infoPanel.add(lblRiverName);

    }

    /**
     * Returns the RiverView panel
     * 
     * @return - the RiverViewPanel
     */
    public JPanel getMainPanel() {
	return this;
    }

    public void setDepth(double depth) {
	this.depth = depth;

	JLabel lbldepth = new JLabel(depth + "");
	lbldepth.setBounds(112, 44, 46, 14);
	lbldepth.setForeground(Color.white);
	infoPanel.add(lbldepth);
    }

    // needs work
    public void onChoiceMade(int choice) {
	this.choice = choice;
	presenter.onChoiceMade();
    }

    // needs work
    public int getChoice() {
	return choice;

    }

    public void setName(String name) {
	JLabel lblName = new JLabel(name);
	lblName.setBounds(112, 13, 90, 20);
	lblName.setForeground(Color.white);
	infoPanel.add(lblName);

    }


   

    private void addBG() {
	riverimage = new JLabel("");

	riverimage
		.setIcon(new ImageIcon(
			StoreBGView.class
				.getResource("/edu/gatech/cs2340/bytebankers/graphics/RiverBGImg.png")));

	riverimage.setBounds(0, 0, 860, 500);

	add(riverimage);
    }

    public void setHasFerry(Boolean boo) 
	{
		if(boo == true)
		{
			btnTakeFerry = new JButton("Take Ferry");
			btnTakeFerry.setBounds(218, 436, 150, 23);
			btnTakeFerry.addActionListener(new ButtonListener());
			add(btnTakeFerry);
			
		}
		
		addBG();
	}
	
	
	public void setMoneyLeft(double remain) 
	{
		moneyLeft = remain;
		
		if(remain >= 250) {
			btnTakeFerry = new JButton("Take Ferry ($250)");
			btnTakeFerry.setBounds(280, 436, 150, 23);
			add(btnTakeFerry);
			btnTakeFerry.addActionListener(new ButtonListener());
			
		}
		
		addBG();
		
		moneyLeft = remain;
		
		JLabel lblMoneyLeft = new JLabel(remain + "");
		lblMoneyLeft.setBounds(112, 69, 70, 14);
		lblMoneyLeft.setForeground(Color.white);
		infoPanel.add(lblMoneyLeft);
	}

	

	

    // Pop up
    public void showResult(String result) {
	JOptionPane.showMessageDialog(this, result);
	onShowResultClose();
    }

    // Pop up
    public void onShowResultClose() {
	presenter.onShowResultClose();
    }

    private class ButtonListener implements ActionListener {
	public void actionPerformed(ActionEvent event) {
	    if (event.getSource() == btnTakeFerry) {
		onChoiceMade(1);
	    } else if (event.getSource() == btnFordRiver) {
		onChoiceMade(2);
	    } else {
		onChoiceMade(3);
	    }
	}
    }
    
    

}
